package com.dragon.www.smarthome.service;

import android.util.Log;

import com.dragon.www.smarthome.model.ActionPacket;
import com.dragon.www.smarthome.net.AppUDPPacket;

import java.nio.ByteBuffer;


/**
 * @version 1.0.0
 * @Class: DevicePacketFactory
 * @Desp: 设备控制包工厂
 * @Author songxy
 * @Date: 2016/3/17  13:55
 */
public final class DevicePacketFactory {

    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_LOGIN = new byte[]{(byte) 0x01, 0x00};
    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_QUIT = new byte[]{(byte) 0x02, 0x00};
    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_DETECT = new byte[]{(byte) 0x03, 0x00};
    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_ACTION = new byte[]{(byte) 0x80, 0x00};
    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_ACTION_DETECT_1 = new byte[]{(byte) 0x81, 0x00};
    public static final byte[] KEY_ACTION_DATAGRAM_TYPE_ACTION_DETECT_2 = new byte[]{(byte) 0x82, 0x00};
    public static byte[] KEY_ACTION_LOGIN_INFO;
    public static byte[] KEY_ACTION_LOGIN_OFF;
    public static byte[] KEY_ACTION_DETECT_STATUS;

    private static ActionPacket loginPacket;
    private static ActionPacket loginOffPacket;
    private static ActionPacket heartPacket;
    private static ActionPacket statusPacket;

    static {
        KEY_ACTION_LOGIN_INFO = new byte[133];
        System.arraycopy("ENTER".getBytes(), 0, KEY_ACTION_LOGIN_INFO, 0, 5);
        ByteBuffer temp = ByteBuffer.allocate(64);
        temp.put("admin".getBytes());
        System.arraycopy(temp.array(), 0, KEY_ACTION_LOGIN_INFO, 5, 64);
        temp = ByteBuffer.allocate(64);
        temp.put("123456".getBytes());
        System.arraycopy(temp.array(), 0, KEY_ACTION_LOGIN_INFO, 69, 64);

        KEY_ACTION_LOGIN_OFF = "QUIT".getBytes();
        KEY_ACTION_DETECT_STATUS = "SYNC".getBytes();

        loginPacket = new ActionPacket((short) 133, KEY_ACTION_DATAGRAM_TYPE_LOGIN, KEY_ACTION_LOGIN_INFO);
        loginOffPacket = new ActionPacket(KEY_ACTION_DATAGRAM_TYPE_QUIT, KEY_ACTION_LOGIN_OFF);
        heartPacket =  new ActionPacket(KEY_ACTION_DATAGRAM_TYPE_DETECT, KEY_ACTION_DETECT_STATUS);
        statusPacket =  new ActionPacket((short) 1, KEY_ACTION_DATAGRAM_TYPE_ACTION_DETECT_1, new byte[]{0});

    }

    private DevicePacketFactory() {
    }

    public static AppUDPPacket getDevicePacket(DevicePacketType type) {
        switch (type) {
            case LOGIN:
               return loginPacket;
            case HEARTBEAT:
                return heartPacket;
            case DEVICESTATUS:
                return statusPacket;
            case ACTION:
                return new ActionPacket();
            case LOGINOFF:
                return loginOffPacket;
            default:
                Log.d("UdpService", "不支持的设备控制包类型");
                return null;
        }
    }

    public enum DevicePacketType {
        /**
         * 登录， 心跳包， 操作， 设备查询
         */
        LOGIN, LOGINOFF, HEARTBEAT, ACTION, DEVICESTATUS;

        @Override
        public String toString() {
            return this.name().toString().toLowerCase();
        }
    }
}
